{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "724c133d-68a6-4adf-bfb3-18afc5507df1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C:\\Users\\16502\\Dropbox\\SettlerNewcomer\\FinalAnalyses\\wvs7.csv\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from samplics.utils.types import PopParam\n",
    "from samplics.categorical import Tabulation, CrossTabulation\n",
    "import warnings\n",
    "#Set your file path here\n",
    "import os\n",
    "thepath=os.getcwd()+r\"\\ivs7.csv\"\n",
    "print(thepath)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b6b4c5bf-d797-4885-988f-136a47274190",
   "metadata": {},
   "outputs": [],
   "source": [
    "#For more information on Integrated Values Survey Wave 7 see Gedeshi, Ilir, David Rotman, Merab Pachulia, Gevorg Poghosyan, and Sylvia Kritzinger. 2022. Joint EVS/WVS 2017–2022 Dataset, ZA7505 Data File Version 4.0.0. Cologne: GESIS. https://doi.org/10.4232/1.14320."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0b0d250e-d62a-42c6-9869-926185ace386",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Country</th>\n",
       "      <th>iso3n</th>\n",
       "      <th>A_YEAR</th>\n",
       "      <th>W_WEIGHT</th>\n",
       "      <th>S018</th>\n",
       "      <th>Q21</th>\n",
       "      <th>Q34</th>\n",
       "      <th>Q34_3</th>\n",
       "      <th>Q63</th>\n",
       "      <th>Q121</th>\n",
       "      <th>...</th>\n",
       "      <th>Q258</th>\n",
       "      <th>Q259</th>\n",
       "      <th>Q260</th>\n",
       "      <th>Q263</th>\n",
       "      <th>Q269</th>\n",
       "      <th>Q275R</th>\n",
       "      <th>Q279</th>\n",
       "      <th>Q288R</th>\n",
       "      <th>Q290</th>\n",
       "      <th>X003R2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Andorra</td>\n",
       "      <td>20</td>\n",
       "      <td>2018</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.996016</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>20001.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Andorra</td>\n",
       "      <td>20</td>\n",
       "      <td>2018</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.996016</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>20001.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Andorra</td>\n",
       "      <td>20</td>\n",
       "      <td>2018</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.996016</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>20001.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Andorra</td>\n",
       "      <td>20</td>\n",
       "      <td>2018</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.996016</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>20001.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Andorra</td>\n",
       "      <td>20</td>\n",
       "      <td>2018</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.996016</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>20001.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 26 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   Country  iso3n  A_YEAR  W_WEIGHT      S018  Q21  Q34  Q34_3  Q63  Q121  \\\n",
       "0  Andorra     20    2018       1.0  0.996016  2.0  2.0    1.0  2.0   5.0   \n",
       "1  Andorra     20    2018       1.0  0.996016  2.0  2.0    1.0  2.0   4.0   \n",
       "2  Andorra     20    2018       1.0  0.996016  2.0  4.0    2.0  3.0   4.0   \n",
       "3  Andorra     20    2018       1.0  0.996016  2.0  2.0    1.0  3.0   4.0   \n",
       "4  Andorra     20    2018       1.0  0.996016  2.0  4.0    2.0  3.0   4.0   \n",
       "\n",
       "   ...  Q258  Q259  Q260  Q263  Q269  Q275R  Q279  Q288R     Q290  X003R2  \n",
       "0  ...   1.0   1.0   2.0   2.0   2.0    2.0   1.0    2.0  20001.0     3.0  \n",
       "1  ...   1.0   1.0   1.0   1.0   1.0    3.0   1.0    3.0  20001.0     2.0  \n",
       "2  ...   2.0   2.0   1.0   2.0   2.0    3.0   1.0    2.0  20001.0     2.0  \n",
       "3  ...   2.0   2.0   2.0   2.0   2.0    1.0   1.0    2.0  20001.0     3.0  \n",
       "4  ...   2.0   2.0   1.0   2.0   2.0    1.0   1.0    2.0  20001.0     2.0  \n",
       "\n",
       "[5 rows x 26 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ivs7=pd.read_csv(thepath,index_col=0)\n",
    "ivs7.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "243ab451-e100-4854-b064-277f98debd4a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Year\n",
       "2018    37445\n",
       "2020    21187\n",
       "2017     9722\n",
       "2022     9350\n",
       "2019     5984\n",
       "2021     5918\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ivs7['weight']=ivs7['W_WEIGHT'] #Weights for by-country summary statistics\n",
    "ivs7['cweight']=ivs7['W_WEIGHT']*ivs7['S018'] #Weights for full sample summary statistics\n",
    "ivs7['Year']=ivs7['A_YEAR']\n",
    "ivs7.Year.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8a1d32fc-bafa-4df0-86f4-3e9016e9969b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "59"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(ivs7.loc[pd.notna(ivs7.Q130),'Country'].unique().tolist())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3a5604be-3d6d-4534-858b-80bc01f328be",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Immigration policy preferences\n",
    "# Q130 How about people from other countries coming here to work. Which one of the following\n",
    "# do you think the government should do?\n",
    "# 1.- Let anyone come who wants to\n",
    "# 2.- Let people come as long as there are jobs available\n",
    "# 3.- Place strict limits on the number of foreigners who can come here\n",
    "# 4.- Prohibit people coming here from other countries\n",
    "# -1-.- Don ́t know\n",
    "# -2-.- No answer\n",
    "# -4-.- Not asked\n",
    "# -5-.- Missing; Not availabl\n",
    "conditions=[\n",
    "    (ivs7['Q130']==1),\n",
    "    (ivs7['Q130']==2),\n",
    "    (ivs7['Q130']==3),\n",
    "    (ivs7['Q130']==4)\n",
    "]\n",
    "choices = ['all','ifjobavail','limit','none']\n",
    "ivs7['immig_econ_level']=np.select(conditions,choices,default=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "64f77275-3101-4f44-978c-4d55041225b1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Immigration and nationality status\n",
    "# Q263 Respondent immigrant\n",
    "# Were you born in this country or are you an immigrant?\n",
    "# 1.- I am born in this country\n",
    "# 2.- I am an immigrant to this country (born outside this country)\n",
    "# Q269\n",
    "# Respondent citizen\n",
    "# Are you a citizen of this country?\n",
    "# 1.- Yes, I am a citizen of this country \n",
    "# 2.- Not, I am not a citizen of this country\n",
    "# 3.- SWI2: Applied, awaiting decision \n",
    "conditions = [\n",
    "  (ivs7['Q269']==1)\n",
    ", (ivs7['Q269']==2)|(ivs7['Q269']==3)\n",
    "]\n",
    "choices=['National','Non-national']\n",
    "ivs7['citizen'] = np.select(conditions, choices,default=None)\n",
    "conditions=[\n",
    "    (ivs7['citizen']=='National')&(ivs7['Q263']==2) #ntrlzd\n",
    "    ,(ivs7['citizen']=='National')&(ivs7['Q263']==1) #locally born citizen\n",
    "    ,(ivs7['citizen']=='Non-national')\n",
    "]\n",
    "choices=['Naturalized','Locally-born national','Non-national']\n",
    "ivs7['ntrlzd']=np.select(conditions,choices,default=None)\n",
    "choices=['Born abroad','Born locally','Born abroad']\n",
    "ivs7['bpl']=np.select(conditions,choices,default=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "df42a0a3-b231-4d64-af99-1b688f373a1c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Racial and ethnic belonging (Q290)\n",
    "# This question is country-specific which includes both levels of the characteristic (coding either race or ethnic belonging depending on the country) and the adaptation of the scale by each survey team. \n",
    "# Races are coded using single cross-country scale while for ethnicity every country proposed their own scale. Subsequent versions of the WVS data-set might include further updates.\n",
    "ivs7['ethnic']=pd.to_numeric(ivs7['Q290'])\n",
    "# #Ethnicity information\n",
    "df=ivs7.loc[(ivs7.ethnic>0),['Country','Year','ethnic']].copy().groupby(['Year','Country'])['ethnic'].agg(pd.Series.mode).reset_index()\n",
    "df.rename(columns={'ethnic':'mostcommon_ethnic'},inplace=True)\n",
    "ivs7=ivs7.merge(df,how='left')\n",
    "conditions=[\n",
    "     ivs7['ethnic']==ivs7['mostcommon_ethnic'],\n",
    "     ivs7['ethnic']!=ivs7['mostcommon_ethnic']\n",
    "]\n",
    "choices=['Ethnic plurality','Ethnic minority']\n",
    "ivs7['ethnicplu']=np.select(conditions,choices,default=None)\n",
    "for i in ['citizen','ntrlzd','bpl']:\n",
    "    ivs7[i+'Xeth']=ivs7[i].astype(str)+', '+ivs7['ethnicplu'].astype(str)\n",
    "    ivs7.loc[((pd.isna(ivs7[i]))|(pd.isna(ivs7['ethnicplu']))),i+'Xeth']=np.nan"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "caeed27f-f828-4b5d-bee4-f816546d927c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Q254 How proud are you to be of nationality of this country?\n",
    "# 1.- Very proud 2.- Quite proud 3.- Not very proud 4.- Not at all proud 5.- I am not [nationality] -1-.- Don´t know -2-.- No answer -4-.- Not asked -5-.- Missing; Not available\n",
    "\n",
    "conditions = [\n",
    "  (ivs7['Q254']==1)|(ivs7['Q254']==2) # & (profit / sales > thr_margin)\n",
    ", (ivs7['Q254']==3)|(ivs7['Q254']==4) #& (profit / sales <= thr_margin)\n",
    ", (ivs7['Q254']==5)\n",
    "]# List of values to return\n",
    "choices  = [\n",
    "      \"Proud\"\n",
    "    , \"Not proud\"\n",
    "    , \"Other nationality\"\n",
    "]\n",
    "ivs7['ntnlpride'] = np.select(conditions, choices,default=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "49de245c-4e21-489f-a554-5eb48a5de4e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Q63\n",
    "# Trust: People of another nationality\n",
    "# I ‘d like to ask you how much you trust people from various groups. Could you tell me for each whether you trust people from this group completely, somewhat, not very much or not at all? People of another nationality\n",
    "# 1.- Trust completely 2.- Trust somewhat 3.- Do not trust very much 4.- Do not trust at all -1-.- Don´t know -2-.- No answer -4-.- Not asked -5-.- Missing; Not available\n",
    "conditions = [\n",
    "      (ivs7['Q63']==1),\n",
    "        (ivs7['Q63']==2),\n",
    "        (ivs7['Q63']==3),\n",
    "        (ivs7['Q63']==4)\n",
    "    ]\n",
    "choices  = ['Trust foreigners','Trust foreigners','Don\\'t trust foreigners','Don\\'t trust foreigners']\n",
    "ivs7['trust_foreigners'] = np.select(conditions, choices,default=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "0c755af7-8536-4422-b6c8-721724cb4d92",
   "metadata": {},
   "outputs": [],
   "source": [
    "#Demographics: Sex, age, education, income, employment\n",
    "ivs7['sex']=ivs7['Q260'].map({2:'Female',1:'Male'})\n",
    "ivs7['age'] = ivs7['X003R2'].map({1:'16-29',2:'30-49',3:'50+'})\n",
    "ivs7['edu']=ivs7['Q275R'].map({1:'Lower education',2:'Intermediate education',3:'Higher education'})\n",
    "ivs7['income']=ivs7['Q288R'].map({1:'Lower income',2:'Intermediate income',3:'Higher income'})\n",
    "# Q279 Employment status\n",
    "# Are you employed now or not? If yes: About how many hours a week?\n",
    "# 1.- Full time (30 hours a week or more)\n",
    "# 2.- Part time (less than 30 hours a week)\n",
    "# 3.- Self employed\n",
    "# 4.- Retired/pensioned\n",
    "# 5.- Housewife not otherwise employed\n",
    "# 6.- Student\n",
    "# 7.- Unemployed\n",
    "# 8.- Other\n",
    "# -1-.- Don´t know\n",
    "# -2-.- No answer\n",
    "# -4-.- Not asked\n",
    "# -5-.- Missing; Not available\n",
    "conditions=[\n",
    "       (ivs7.Q279.isin([1,2,3])),\n",
    "    (ivs7.Q279==7),\n",
    "       (ivs7.Q279.isin([4,5,6,8]))\n",
    "]\n",
    "choices=['Employed','Unemployed','Not in workforce']\n",
    "ivs7['employment']=np.select(conditions,choices,default=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "cd2fba8f-c333-4221-9a71-5dd2fb96f7b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "#Economic ideology\n",
    "#Q240 - In political matters, people talk of \"the left\" and \"the right.\" How would you place your views on this scale, generally speaking?\n",
    "conditions=[\n",
    "    ivs7['Q240']<=5,\n",
    "    ivs7['Q240']>=6\n",
    "]\n",
    "choices=['Left ideology','Right ideology']\n",
    "ivs7['ideology']=np.select(conditions,choices,default=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "551d3c73-1169-496c-bf9b-6f2812172f53",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Support for pro-national hiring\n",
    "#Q34 Do you agree, disagree or neither agree nor disagree with the following statements? -\n",
    "# When jobs are scarce, employers should give priority to people of this country over\n",
    "# immigrants.\n",
    "conditions=[\n",
    "    (ivs7['Q34']==1)|(ivs7['Q34']==2)|(ivs7['Q34_3']==1),\n",
    "    (ivs7['Q34']==3)|(ivs7['Q34_3']==3),\n",
    "    (ivs7['Q34']==4)|(ivs7['Q34']==5)|(ivs7['Q34_3']==2),\n",
    "]\n",
    "\n",
    "choices=['Agree','Neutral','Disagree']\n",
    "ivs7['jobs']=np.select(conditions,choices,default=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "6438a67b-f1d2-4d65-b42d-a6a2cfee7ee0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>subntnlmin</th>\n",
       "      <th>intlmin</th>\n",
       "      <th>Q257</th>\n",
       "      <th>placemin</th>\n",
       "      <th>placeid</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [subntnlmin, intlmin, Q257, placemin, placeid]\n",
       "Index: []"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Place feels closest to\n",
    "ivs7['subntnlmin'] = ivs7[['Q255','Q256']].min(axis=1)\n",
    "ivs7['intlmin'] = ivs7[['Q258','Q259']].min(axis=1)\n",
    "ivs7['placemin']=ivs7[['subntnlmin','intlmin','Q257']].min(axis=1)\n",
    "conditions=[\n",
    "    (ivs7['placemin']==4), #close nowhere\n",
    "    (ivs7['placemin']==3)&(ivs7['placemin']==ivs7['subntnlmin'])&(ivs7['placemin']==ivs7['Q257'])&(ivs7['placemin']==ivs7['intlmin']), #close nowhere\n",
    "    (ivs7['placemin']<3)&(ivs7['placemin']==ivs7['subntnlmin'])&(ivs7['placemin']==ivs7['Q257'])&(ivs7['placemin']==ivs7['intlmin']), #closest all\n",
    "    (ivs7['placemin']!=4)&(ivs7['placemin']==ivs7['subntnlmin'])&(ivs7['placemin']!=ivs7['Q257'])&(ivs7['placemin']!=ivs7['intlmin']), #closest sub   \n",
    "    (ivs7['placemin']!=4)&(ivs7['placemin']==ivs7['subntnlmin'])&(ivs7['placemin']==ivs7['Q257'])&(ivs7['placemin']!=ivs7['intlmin']), #closest sub + cntry\n",
    "    (ivs7['placemin']!=4)&(ivs7['placemin']==ivs7['subntnlmin'])&(ivs7['placemin']!=ivs7['Q257'])&(ivs7['placemin']==ivs7['intlmin']), #closest sub + intl\n",
    "    (ivs7['placemin']!=4)&(ivs7['placemin']!=ivs7['subntnlmin'])&(ivs7['placemin']==ivs7['Q257'])&(ivs7['placemin']!=ivs7['intlmin']), #closest cntry\n",
    "    (ivs7['placemin']!=4)&(ivs7['placemin']!=ivs7['subntnlmin'])&(ivs7['placemin']==ivs7['Q257'])&(ivs7['placemin']==ivs7['intlmin']), #closest cntry and intl\n",
    "   (ivs7['placemin']!=4)&(ivs7['placemin']!=ivs7['subntnlmin'])&(ivs7['placemin']!=ivs7['Q257'])&(ivs7['placemin']==ivs7['intlmin']) #cintl\n",
    "]\n",
    "\n",
    "choices=[\n",
    "    'Not close anywhere','Not close anywhere','Equally close all','Closest sub-national',\n",
    "    'Closest sub-national and national','Closest sub-national and international','Closest national','Closest international and national','Closest international'\n",
    "]\n",
    "\n",
    "ivs7['placeid']=np.select(conditions,choices,default=None)\n",
    "ivs7.loc[(pd.notna(ivs7.placemin))&(pd.isna(ivs7.placeid)),['subntnlmin','intlmin','Q257','placemin','placeid']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "dbecfd00-8e07-402c-a8cd-eed3cde52222",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Perceived immigration impact\n",
    "# Q121\n",
    "# Now we would like to know your opinion about the people from other countries who come\n",
    "# to live in this country - the immigrants. How would you evaluate the impact of these\n",
    "# people on the development of this country?\n",
    "# 1.- Rather bad\n",
    "# 2.- Quite bad\n",
    "# 3.- Neither good, nor bad\n",
    "# 4.- Quite good\n",
    "# 5.- Very good\n",
    "# -1-.- Don ́t know\n",
    "# -2-.- No answer\n",
    "# -4-.- Not asked\n",
    "# -5-.- Missing; Not available\n",
    "\n",
    "conditions = [\n",
    "  (ivs7['Q121']==1)|(ivs7['Q121']==2)\n",
    ", (ivs7['Q121']==3)\n",
    ", (ivs7['Q121']==4)|(ivs7['Q121']==5)\n",
    "]\n",
    "choices  = [\n",
    "      \"Bad\"\n",
    "    , \"Neutral\"\n",
    "    , \"Good\"\n",
    "]\n",
    "ivs7['impact'] = np.select(conditions, choices,default=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "68262bdf-519c-414d-afae-bba9e3ccc835",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Whether foreigners OK\n",
    "# Q21 - Foreigners as neighbors\n",
    "# On this list are various groups of people. Could you please mention any that you would not like to have as neighbors? – Immigrants/foreign workers\n",
    "# 1.- Mentioned 2.- Not mentioned -1-.- Don´t know -2-.- No answer -4-.- Not asked -5-.- Missing; Not available\n",
    "\n",
    "ivs7['foreignneighbors_OK'] = ivs7['Q21'].map({1:'Foreign neighbor not OK',2:'Foreign neighbor OK'})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0a25831-97c2-40bc-8886-320077e09cb8",
   "metadata": {},
   "source": [
    "#### Making Figure 2.2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "eb2bf883-81f2-4ce2-9279-1084623da973",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>group</th>\n",
       "      <th>group_count</th>\n",
       "      <th>pc_all</th>\n",
       "      <th>pc_ifjobavail</th>\n",
       "      <th>pc_limit</th>\n",
       "      <th>pc_none</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>All</td>\n",
       "      <td>84361.0</td>\n",
       "      <td>10.0966</td>\n",
       "      <td>32.369282</td>\n",
       "      <td>47.280654</td>\n",
       "      <td>10.253464</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  group  group_count   pc_all  pc_ifjobavail   pc_limit    pc_none\n",
       "0   All      84361.0  10.0966      32.369282  47.280654  10.253464"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def ivs7function(avar,groupers=None,thew='cweight',df=ivs7,excludena=True):\n",
    "    use=df.copy()\n",
    "    if excludena is True:\n",
    "        use=use.loc[pd.notna(use[avar])]\n",
    "    if groupers is None:\n",
    "        groupers=['group']\n",
    "        use['group']='All'\n",
    "    use['groupsum']=use.groupby(groupers,observed=True)[thew].transform('sum')\n",
    "    use['pc']=use[thew]/use['groupsum']*100\n",
    "    use['group_count']=use.groupby(groupers,observed=True)[avar].transform('count')\n",
    "    partway=use.groupby(groupers+[avar],observed=True).agg({'pc' : 'sum', 'group_count':'mean'}).reset_index()\n",
    "    partway=partway.loc[(pd.notna(partway['group_count']))].copy()\n",
    "    toreturn=partway.copy()  #bug - https://stackoverflow.com/questions/59628014/pandas-pivot-table-is-giving-the-error-valueerror-the-name-none-occurs-multiple\n",
    "    l2=groupers+['group_count']\n",
    "    toreturn=toreturn.pivot(index=l2, columns=avar, values=['pc']).reset_index()\n",
    "    toreturn.columns = ['_'.join(str(x) for x in col_name).rstrip('_') if len(col_name)==2 else col_name for col_name in toreturn.columns.to_flat_index()]\n",
    "    return(toreturn)\n",
    "    \n",
    "ivs7function('immig_econ_level')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "716b5bfb-c90a-4034-9879-6d5a013c0f61",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>group</th>\n",
       "      <th>group_count</th>\n",
       "      <th>pc_all</th>\n",
       "      <th>pc_ifjobavail</th>\n",
       "      <th>pc_limit</th>\n",
       "      <th>pc_none</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>All respondents</td>\n",
       "      <td>84361.0</td>\n",
       "      <td>10.096600</td>\n",
       "      <td>32.369282</td>\n",
       "      <td>47.280654</td>\n",
       "      <td>10.253464</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Locally-born national</td>\n",
       "      <td>77814.0</td>\n",
       "      <td>10.216906</td>\n",
       "      <td>31.191111</td>\n",
       "      <td>47.830097</td>\n",
       "      <td>10.761886</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Naturalized</td>\n",
       "      <td>2605.0</td>\n",
       "      <td>8.849858</td>\n",
       "      <td>41.011235</td>\n",
       "      <td>44.511136</td>\n",
       "      <td>5.627771</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Non-national</td>\n",
       "      <td>1847.0</td>\n",
       "      <td>8.276119</td>\n",
       "      <td>60.792813</td>\n",
       "      <td>28.734515</td>\n",
       "      <td>2.196554</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Ethnic minority</td>\n",
       "      <td>28044.0</td>\n",
       "      <td>9.073117</td>\n",
       "      <td>32.962512</td>\n",
       "      <td>46.332275</td>\n",
       "      <td>11.632096</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Ethnic plurality</td>\n",
       "      <td>56317.0</td>\n",
       "      <td>10.577480</td>\n",
       "      <td>32.090555</td>\n",
       "      <td>47.726246</td>\n",
       "      <td>9.605719</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Not proud</td>\n",
       "      <td>8754.0</td>\n",
       "      <td>11.537546</td>\n",
       "      <td>33.568474</td>\n",
       "      <td>44.725170</td>\n",
       "      <td>10.168810</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Other nationality</td>\n",
       "      <td>1452.0</td>\n",
       "      <td>9.328090</td>\n",
       "      <td>60.167404</td>\n",
       "      <td>28.414210</td>\n",
       "      <td>2.090297</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Proud</td>\n",
       "      <td>73256.0</td>\n",
       "      <td>9.907505</td>\n",
       "      <td>31.573927</td>\n",
       "      <td>48.037569</td>\n",
       "      <td>10.480999</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>Don't trust foreigners</td>\n",
       "      <td>47943.0</td>\n",
       "      <td>8.854894</td>\n",
       "      <td>28.309760</td>\n",
       "      <td>49.749657</td>\n",
       "      <td>13.085688</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>Trust foreigners</td>\n",
       "      <td>33137.0</td>\n",
       "      <td>12.185678</td>\n",
       "      <td>38.663674</td>\n",
       "      <td>43.258038</td>\n",
       "      <td>5.892610</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     group  group_count     pc_all  pc_ifjobavail   pc_limit  \\\n",
       "0          All respondents      84361.0  10.096600      32.369282  47.280654   \n",
       "1    Locally-born national      77814.0  10.216906      31.191111  47.830097   \n",
       "2              Naturalized       2605.0   8.849858      41.011235  44.511136   \n",
       "3             Non-national       1847.0   8.276119      60.792813  28.734515   \n",
       "4          Ethnic minority      28044.0   9.073117      32.962512  46.332275   \n",
       "5         Ethnic plurality      56317.0  10.577480      32.090555  47.726246   \n",
       "6                Not proud       8754.0  11.537546      33.568474  44.725170   \n",
       "7        Other nationality       1452.0   9.328090      60.167404  28.414210   \n",
       "8                    Proud      73256.0   9.907505      31.573927  48.037569   \n",
       "9   Don't trust foreigners      47943.0   8.854894      28.309760  49.749657   \n",
       "10        Trust foreigners      33137.0  12.185678      38.663674  43.258038   \n",
       "\n",
       "      pc_none  \n",
       "0   10.253464  \n",
       "1   10.761886  \n",
       "2    5.627771  \n",
       "3    2.196554  \n",
       "4   11.632096  \n",
       "5    9.605719  \n",
       "6   10.168810  \n",
       "7    2.090297  \n",
       "8   10.480999  \n",
       "9   13.085688  \n",
       "10   5.892610  "
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Figure 2.2\n",
    "tographA=ivs7function('immig_econ_level')\n",
    "tographA['group']='All respondents'\n",
    "\n",
    "tograph1=[tographA]\n",
    "\n",
    "for x in ['ntrlzd','ethnicplu','ntnlpride','trust_foreigners'\n",
    "            ]:\n",
    "    g=ivs7function('immig_econ_level',[x])\n",
    "    g.rename(columns={x:'group'},inplace=True)\n",
    "    tograph1=tograph1+[g]\n",
    "\n",
    "tograph2=pd.concat(tograph1,ignore_index=True)\n",
    "tograph2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "1a4fd506-9487-40fb-bfef-e7212448aef9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>group_count</th>\n",
       "      <th>pc_all</th>\n",
       "      <th>pc_ifjobavail</th>\n",
       "      <th>pc_limit</th>\n",
       "      <th>pc_none</th>\n",
       "      <th>ifavailORlimit</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>59.000000</td>\n",
       "      <td>59.000000</td>\n",
       "      <td>59.000000</td>\n",
       "      <td>59.000000</td>\n",
       "      <td>59.000000</td>\n",
       "      <td>59.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>1429.847458</td>\n",
       "      <td>10.111020</td>\n",
       "      <td>32.398348</td>\n",
       "      <td>47.260308</td>\n",
       "      <td>10.230324</td>\n",
       "      <td>79.658656</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>555.697288</td>\n",
       "      <td>7.214377</td>\n",
       "      <td>12.655861</td>\n",
       "      <td>13.316709</td>\n",
       "      <td>6.984458</td>\n",
       "      <td>9.708382</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>957.000000</td>\n",
       "      <td>1.572327</td>\n",
       "      <td>5.777055</td>\n",
       "      <td>23.016094</td>\n",
       "      <td>0.864780</td>\n",
       "      <td>49.389748</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>1173.500000</td>\n",
       "      <td>5.077131</td>\n",
       "      <td>23.923263</td>\n",
       "      <td>37.906566</td>\n",
       "      <td>4.941536</td>\n",
       "      <td>74.844571</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>1200.000000</td>\n",
       "      <td>8.750000</td>\n",
       "      <td>29.789104</td>\n",
       "      <td>44.984586</td>\n",
       "      <td>8.554320</td>\n",
       "      <td>78.440592</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>1504.000000</td>\n",
       "      <td>13.748563</td>\n",
       "      <td>41.892582</td>\n",
       "      <td>56.456196</td>\n",
       "      <td>15.284202</td>\n",
       "      <td>87.861709</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>4018.000000</td>\n",
       "      <td>40.602116</td>\n",
       "      <td>67.244898</td>\n",
       "      <td>76.818864</td>\n",
       "      <td>25.741579</td>\n",
       "      <td>97.562893</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       group_count     pc_all  pc_ifjobavail   pc_limit    pc_none  \\\n",
       "count    59.000000  59.000000      59.000000  59.000000  59.000000   \n",
       "mean   1429.847458  10.111020      32.398348  47.260308  10.230324   \n",
       "std     555.697288   7.214377      12.655861  13.316709   6.984458   \n",
       "min     957.000000   1.572327       5.777055  23.016094   0.864780   \n",
       "25%    1173.500000   5.077131      23.923263  37.906566   4.941536   \n",
       "50%    1200.000000   8.750000      29.789104  44.984586   8.554320   \n",
       "75%    1504.000000  13.748563      41.892582  56.456196  15.284202   \n",
       "max    4018.000000  40.602116      67.244898  76.818864  25.741579   \n",
       "\n",
       "       ifavailORlimit  \n",
       "count       59.000000  \n",
       "mean        79.658656  \n",
       "std          9.708382  \n",
       "min         49.389748  \n",
       "25%         74.844571  \n",
       "50%         78.440592  \n",
       "75%         87.861709  \n",
       "max         97.562893  "
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Calculate results by country\n",
    "c=ivs7function('immig_econ_level',['Country'],thew='weight')\n",
    "c['ifavailORlimit']=c['pc_ifjobavail']+c['pc_limit']\n",
    "c.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "e266423b-13c9-40c4-9857-0aa32bedf64d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Country</th>\n",
       "      <th>group_count</th>\n",
       "      <th>pc_all</th>\n",
       "      <th>pc_ifjobavail</th>\n",
       "      <th>pc_limit</th>\n",
       "      <th>pc_none</th>\n",
       "      <th>ifavailORlimit</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>Morocco</td>\n",
       "      <td>1200.0</td>\n",
       "      <td>19.000000</td>\n",
       "      <td>38.583333</td>\n",
       "      <td>37.833333</td>\n",
       "      <td>4.583333</td>\n",
       "      <td>76.416667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Armenia</td>\n",
       "      <td>1187.0</td>\n",
       "      <td>22.286495</td>\n",
       "      <td>46.659391</td>\n",
       "      <td>23.016094</td>\n",
       "      <td>8.038020</td>\n",
       "      <td>69.675485</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>55</th>\n",
       "      <td>Uruguay</td>\n",
       "      <td>957.0</td>\n",
       "      <td>26.190655</td>\n",
       "      <td>42.374985</td>\n",
       "      <td>25.136146</td>\n",
       "      <td>6.298215</td>\n",
       "      <td>67.511131</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Bangladesh</td>\n",
       "      <td>1138.0</td>\n",
       "      <td>26.625659</td>\n",
       "      <td>29.789104</td>\n",
       "      <td>31.810193</td>\n",
       "      <td>11.775044</td>\n",
       "      <td>61.599297</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>Guatemala</td>\n",
       "      <td>1229.0</td>\n",
       "      <td>40.602116</td>\n",
       "      <td>5.777055</td>\n",
       "      <td>43.612693</td>\n",
       "      <td>10.008137</td>\n",
       "      <td>49.389748</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       Country  group_count     pc_all  pc_ifjobavail   pc_limit    pc_none  \\\n",
       "33     Morocco       1200.0  19.000000      38.583333  37.833333   4.583333   \n",
       "2      Armenia       1187.0  22.286495      46.659391  23.016094   8.038020   \n",
       "55     Uruguay        957.0  26.190655      42.374985  25.136146   6.298215   \n",
       "4   Bangladesh       1138.0  26.625659      29.789104  31.810193  11.775044   \n",
       "18   Guatemala       1229.0  40.602116       5.777055  43.612693  10.008137   \n",
       "\n",
       "    ifavailORlimit  \n",
       "33       76.416667  \n",
       "2        69.675485  \n",
       "55       67.511131  \n",
       "4        61.599297  \n",
       "18       49.389748  "
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Most liberal countries\n",
    "c.sort_values(by=['pc_all']).tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "80c7b77c-b85d-4a0b-848f-e4f354aef648",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Country</th>\n",
       "      <th>group_count</th>\n",
       "      <th>pc_all</th>\n",
       "      <th>pc_ifjobavail</th>\n",
       "      <th>pc_limit</th>\n",
       "      <th>pc_none</th>\n",
       "      <th>ifavailORlimit</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>Kyrgyzstan</td>\n",
       "      <td>1158.0</td>\n",
       "      <td>10.974888</td>\n",
       "      <td>28.840910</td>\n",
       "      <td>41.517932</td>\n",
       "      <td>18.666269</td>\n",
       "      <td>70.358843</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>Jordan</td>\n",
       "      <td>1178.0</td>\n",
       "      <td>6.112054</td>\n",
       "      <td>17.232598</td>\n",
       "      <td>57.130730</td>\n",
       "      <td>19.524618</td>\n",
       "      <td>74.363328</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>Myanmar</td>\n",
       "      <td>1200.0</td>\n",
       "      <td>8.750000</td>\n",
       "      <td>23.666667</td>\n",
       "      <td>46.750000</td>\n",
       "      <td>20.833333</td>\n",
       "      <td>70.416667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>Ecuador</td>\n",
       "      <td>1162.0</td>\n",
       "      <td>6.196213</td>\n",
       "      <td>27.022375</td>\n",
       "      <td>43.459552</td>\n",
       "      <td>23.321859</td>\n",
       "      <td>70.481928</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>Iraq</td>\n",
       "      <td>1179.0</td>\n",
       "      <td>6.276506</td>\n",
       "      <td>24.088210</td>\n",
       "      <td>45.716709</td>\n",
       "      <td>23.918575</td>\n",
       "      <td>69.804919</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>52</th>\n",
       "      <td>Turkey</td>\n",
       "      <td>2349.0</td>\n",
       "      <td>5.236271</td>\n",
       "      <td>25.159642</td>\n",
       "      <td>45.466156</td>\n",
       "      <td>24.137931</td>\n",
       "      <td>70.625798</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>39</th>\n",
       "      <td>Pakistan</td>\n",
       "      <td>1902.0</td>\n",
       "      <td>12.618297</td>\n",
       "      <td>28.391167</td>\n",
       "      <td>34.700315</td>\n",
       "      <td>24.290221</td>\n",
       "      <td>63.091483</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Bolivia</td>\n",
       "      <td>1989.0</td>\n",
       "      <td>6.133736</td>\n",
       "      <td>22.775264</td>\n",
       "      <td>45.349422</td>\n",
       "      <td>25.741579</td>\n",
       "      <td>68.124686</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       Country  group_count     pc_all  pc_ifjobavail   pc_limit    pc_none  \\\n",
       "26  Kyrgyzstan       1158.0  10.974888      28.840910  41.517932  18.666269   \n",
       "23      Jordan       1178.0   6.112054      17.232598  57.130730  19.524618   \n",
       "34     Myanmar       1200.0   8.750000      23.666667  46.750000  20.833333   \n",
       "12     Ecuador       1162.0   6.196213      27.022375  43.459552  23.321859   \n",
       "21        Iraq       1179.0   6.276506      24.088210  45.716709  23.918575   \n",
       "52      Turkey       2349.0   5.236271      25.159642  45.466156  24.137931   \n",
       "39    Pakistan       1902.0  12.618297      28.391167  34.700315  24.290221   \n",
       "5      Bolivia       1989.0   6.133736      22.775264  45.349422  25.741579   \n",
       "\n",
       "    ifavailORlimit  \n",
       "26       70.358843  \n",
       "23       74.363328  \n",
       "34       70.416667  \n",
       "12       70.481928  \n",
       "21       69.804919  \n",
       "52       70.625798  \n",
       "39       63.091483  \n",
       "5        68.124686  "
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Least liberal countries\n",
    "c.sort_values(by=['pc_none']).tail(8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "9681754e-2b95-46ff-bfd7-834a8ada15a5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "        Country  group_count    pc_all  pc_ifjobavail   pc_limit   pc_none  \\\n",
      "45    Singapore       1982.0  1.907795      23.758317  70.244707  4.089182   \n",
      "0       Andorra        980.0  4.693878      67.244898  26.938776  1.122449   \n",
      "48       Taiwan       1212.0  2.925939      17.955521  76.818864  2.299676   \n",
      "36  New Zealand        996.0  2.309237      52.208835  44.578313  0.903614   \n",
      "22        Japan       1272.0  1.572327      41.902516  55.660377  0.864780   \n",
      "\n",
      "    ifavailORlimit  \n",
      "45       94.003023  \n",
      "0        94.183673  \n",
      "48       94.774385  \n",
      "36       96.787149  \n",
      "22       97.562893  \n",
      "       Country  group_count     pc_all  pc_ifjobavail   pc_limit    pc_none  \\\n",
      "18   Guatemala       1229.0  40.602116       5.777055  43.612693  10.008137   \n",
      "4   Bangladesh       1138.0  26.625659      29.789104  31.810193  11.775044   \n",
      "39    Pakistan       1902.0  12.618297      28.391167  34.700315  24.290221   \n",
      "55     Uruguay        957.0  26.190655      42.374985  25.136146   6.298215   \n",
      "14    Ethiopia       1213.0  17.642209      30.585326  37.015664  14.756801   \n",
      "\n",
      "    ifavailORlimit  \n",
      "18       49.389748  \n",
      "4        61.599297  \n",
      "39       63.091483  \n",
      "55       67.511131  \n",
      "14       67.600989  \n"
     ]
    }
   ],
   "source": [
    "#Support for intermediate policy options\n",
    "print(c.sort_values(by=['ifavailORlimit']).tail())\n",
    "print(c.sort_values(by=['ifavailORlimit']).head())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d1cd96f-f5ce-496b-8014-67fa317e930d",
   "metadata": {},
   "source": [
    "#### Make Table A.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "60b35ab6-35be-4b6c-8cc8-d0c3e9eb9376",
   "metadata": {},
   "outputs": [],
   "source": [
    "def makeappendtable(avar,dv='immig_econ_level'):\n",
    "    j=ivs7function(dv,[avar])\n",
    "    print(\"FULL SAMPLE RESULTS\")\n",
    "    print(j)\n",
    "    ctab=CrossTabulation(param=PopParam.prop)\n",
    "    ctab.tabulate(\n",
    "        vars=ivs7[[avar, \"immig_econ_level\"]],\n",
    "        samp_weight=ivs7['cweight'],\n",
    "        remove_nan=True,\n",
    "    )    \n",
    "    print(ctab)\n",
    "    k=ivs7function(dv,['Country',avar],thew='weight')\n",
    "    use=ivs7.loc[pd.notna(ivs7[dv])].copy().groupby('Country')\n",
    "    toappend=[]\n",
    "    for adf in [use.get_group(x) for x in use.groups]:\n",
    "        adf=adf.loc[((pd.notna(adf[avar]))&(pd.notna(adf[dv])))].copy()\n",
    "        if len(adf)>0:\n",
    "            ctab.tabulate(\n",
    "            vars=adf[[avar, dv]],\n",
    "            samp_weight=adf['weight'],\n",
    "            remove_nan=True,\n",
    "            )\n",
    "            results=pd.DataFrame({'Country':adf.Country.unique().tolist()[0],\n",
    "                                           'chisq':ctab.stats.get('Pearson-Unadj').get('chisq_value'),\n",
    "                                           'pvalue':ctab.stats.get('Pearson-Unadj').get('p_value')\n",
    "                                           },index=[0]\n",
    "                                )\n",
    "            toappend=toappend+[results]\n",
    "    toreturn=k.merge(pd.concat(toappend,ignore_index=True),how='left')\n",
    "    return toreturn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "ba78c81b-9bc9-4562-9613-929cdf328ac7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "FULL SAMPLE RESULTS\n",
      "      sex  group_count     pc_all  pc_ifjobavail   pc_limit    pc_none\n",
      "0  Female      44144.0   9.996758      32.334136  46.912320  10.756786\n",
      "1    Male      40140.0  10.203327      32.385890  47.675475   9.735308\n",
      "\n",
      "Cross-tabulation of sex and immig_econ_level\n",
      " Number of strata: 1\n",
      " Number of PSUs: 84284\n",
      " Number of observations: 84284\n",
      " Degrees of freedom: 84283.00\n",
      "\n",
      "    sex immig_econ_level  point_est  stderror  lower_ci  upper_ci\n",
      "Female              all   0.051379  0.000822  0.049792  0.053013\n",
      "Female       ifjobavail   0.166184  0.001401  0.163455  0.168949\n",
      "Female            limit   0.241109  0.001621  0.237947  0.244300\n",
      "Female             none   0.055285  0.000859  0.053625  0.056994\n",
      "  Male              all   0.049593  0.000852  0.047949  0.051289\n",
      "  Male       ifjobavail   0.157409  0.001454  0.154580  0.160281\n",
      "  Male            limit   0.231723  0.001610  0.228584  0.234893\n",
      "  Male             none   0.047318  0.000803  0.045768  0.048917\n",
      "\n",
      "Pearson (with Rao-Scott adjustment):\n",
      "\tUnadjusted - chi2(3): 24.9119 with p-value of 0.0000\n",
      "\tAdjusted - F(0.00, 0.00): 0.0000  with p-value of nan\n",
      "\n",
      "  Likelihood ratio (with Rao-Scott adjustment):\n",
      "\t Unadjusted - chi2(3): 24.9401 with p-value of 0.0000\n",
      "\t Adjusted - F(0.00, 0.00): 0.0000  with p-value of nan\n",
      "\n"
     ]
    }
   ],
   "source": [
    "with warnings.catch_warnings(action=\"ignore\"): #Suppress warnings about Country samples with missing categories\n",
    "    makeappendtable('sex')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "0d951f35-f82c-4fa8-baa2-1c530a655340",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "FULL SAMPLE RESULTS\n",
      "     age  group_count     pc_all  pc_ifjobavail   pc_limit    pc_none\n",
      "0  16-29      21302.0  13.070734      33.094754  44.335177   9.499335\n",
      "1  30-49      33570.0  10.243393      31.995523  47.474722  10.286362\n",
      "2    50+      29261.0   7.514139      32.155685  49.456999  10.873177\n",
      "\n",
      "Cross-tabulation of age and immig_econ_level\n",
      " Number of strata: 1\n",
      " Number of PSUs: 84133\n",
      " Number of observations: 84133\n",
      " Degrees of freedom: 84132.00\n",
      "\n",
      "   age immig_econ_level  point_est  stderror  lower_ci  upper_ci\n",
      "16-29              all   0.035209  0.000721  0.033824  0.036649\n",
      "16-29       ifjobavail   0.089149  0.001160  0.086901  0.091449\n",
      "16-29            limit   0.119428  0.001250  0.116999  0.121900\n",
      "16-29             none   0.025589  0.000590  0.024457  0.026771\n",
      "30-49              all   0.040736  0.000757  0.039279  0.042246\n",
      "30-49       ifjobavail   0.127241  0.001286  0.124741  0.129783\n",
      "30-49            limit   0.188799  0.001483  0.185908  0.191723\n",
      "30-49             none   0.040907  0.000747  0.039467  0.042397\n",
      "  50+              all   0.025018  0.000577  0.023911  0.026174\n",
      "  50+       ifjobavail   0.107060  0.001161  0.104806  0.109357\n",
      "  50+            limit   0.164664  0.001400  0.161938  0.167426\n",
      "  50+             none   0.036201  0.000709  0.034838  0.037617\n",
      "\n",
      "Pearson (with Rao-Scott adjustment):\n",
      "\tUnadjusted - chi2(6): 482.7506 with p-value of 0.0000\n",
      "\tAdjusted - F(0.00, 0.00): 0.0000  with p-value of nan\n",
      "\n",
      "  Likelihood ratio (with Rao-Scott adjustment):\n",
      "\t Unadjusted - chi2(6): 484.4631 with p-value of 0.0000\n",
      "\t Adjusted - F(0.00, 0.00): 0.0000  with p-value of nan\n",
      "\n"
     ]
    }
   ],
   "source": [
    "with warnings.catch_warnings(action=\"ignore\"): #Suppress warnings about Country samples with missing categories\n",
    "    makeappendtable('age')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "4ece67d3-f9e8-4541-a63e-4489c312a93d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "FULL SAMPLE RESULTS\n",
      "                      edu  group_count     pc_all  pc_ifjobavail   pc_limit  \\\n",
      "0        Higher education      27796.0  10.521409      34.513590  48.632373   \n",
      "1  Intermediate education      29427.0   9.503382      33.251575  48.111010   \n",
      "2         Lower education      26396.0  10.309242      29.450904  45.183874   \n",
      "\n",
      "     pc_none  \n",
      "0   6.332628  \n",
      "1   9.134034  \n",
      "2  15.055980  \n",
      "\n",
      "Cross-tabulation of edu and immig_econ_level\n",
      " Number of strata: 1\n",
      " Number of PSUs: 83619\n",
      " Number of observations: 83619\n",
      " Degrees of freedom: 83618.00\n",
      "\n",
      "                    edu immig_econ_level  point_est  stderror  lower_ci  upper_ci\n",
      "      Higher education              all   0.032545  0.000677  0.031244  0.033898\n",
      "      Higher education       ifjobavail   0.106758  0.001139  0.104546  0.109011\n",
      "      Higher education            limit   0.150430  0.001332  0.147839  0.153059\n",
      "      Higher education             none   0.019588  0.000511  0.018612  0.020614\n",
      "Intermediate education              all   0.033786  0.000700  0.032440  0.035187\n",
      "Intermediate education       ifjobavail   0.118216  0.001228  0.115830  0.120644\n",
      "Intermediate education            limit   0.171044  0.001428  0.168265  0.173861\n",
      "Intermediate education             none   0.032473  0.000657  0.031209  0.033787\n",
      "       Lower education              all   0.034552  0.000697  0.033212  0.035944\n",
      "       Lower education       ifjobavail   0.098707  0.001257  0.096270  0.101199\n",
      "       Lower education            limit   0.151437  0.001404  0.148705  0.154210\n",
      "       Lower education             none   0.050461  0.000846  0.048829  0.052145\n",
      "\n",
      "Pearson (with Rao-Scott adjustment):\n",
      "\tUnadjusted - chi2(6): 1228.9183 with p-value of 0.0000\n",
      "\tAdjusted - F(0.00, 0.00): 0.0000  with p-value of nan\n",
      "\n",
      "  Likelihood ratio (with Rao-Scott adjustment):\n",
      "\t Unadjusted - chi2(6): 1213.5560 with p-value of 0.0000\n",
      "\t Adjusted - F(0.00, 0.00): 0.0000  with p-value of nan\n",
      "\n"
     ]
    }
   ],
   "source": [
    "with warnings.catch_warnings(action=\"ignore\"): #Suppress warnings about Country samples with missing categories\n",
    "    makeappendtable('edu')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "881f1788-bcec-4c34-a9dc-daf964c0efaa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "FULL SAMPLE RESULTS\n",
      "                income  group_count     pc_all  pc_ifjobavail   pc_limit  \\\n",
      "0        Higher income       8444.0  11.783842      34.058520  44.764885   \n",
      "1  Intermediate income      53409.0  10.271552      32.742453  47.925517   \n",
      "2         Lower income      20159.0   8.806099      30.482274  46.769749   \n",
      "\n",
      "     pc_none  \n",
      "0   9.392753  \n",
      "1   9.060478  \n",
      "2  13.941878  \n",
      "\n",
      "Cross-tabulation of income and immig_econ_level\n",
      " Number of strata: 1\n",
      " Number of PSUs: 82012\n",
      " Number of observations: 82012\n",
      " Degrees of freedom: 82011.00\n",
      "\n",
      "              income immig_econ_level  point_est  stderror  lower_ci  upper_ci\n",
      "      Higher income              all   0.011718  0.000404  0.010951  0.012537\n",
      "      Higher income       ifjobavail   0.033867  0.000686  0.032547  0.035239\n",
      "      Higher income            limit   0.044514  0.000780  0.043009  0.046068\n",
      "      Higher income             none   0.009340  0.000367  0.008647  0.010088\n",
      "Intermediate income              all   0.067064  0.000978  0.065173  0.069006\n",
      "Intermediate income       ifjobavail   0.213780  0.001591  0.210679  0.216914\n",
      "Intermediate income            limit   0.312912  0.001790  0.309416  0.316430\n",
      "Intermediate income             none   0.059157  0.000897  0.057424  0.060939\n",
      "       Lower income              all   0.021808  0.000555  0.020746  0.022923\n",
      "       Lower income       ifjobavail   0.075489  0.001094  0.073372  0.077661\n",
      "       Lower income            limit   0.115824  0.001235  0.113425  0.118267\n",
      "       Lower income             none   0.034527  0.000705  0.033171  0.035936\n",
      "\n",
      "Pearson (with Rao-Scott adjustment):\n",
      "\tUnadjusted - chi2(6): 454.5705 with p-value of 0.0000\n",
      "\tAdjusted - F(0.00, 0.00): 0.0000  with p-value of nan\n",
      "\n",
      "  Likelihood ratio (with Rao-Scott adjustment):\n",
      "\t Unadjusted - chi2(6): 432.5262 with p-value of 0.0000\n",
      "\t Adjusted - F(0.00, 0.00): 0.0000  with p-value of nan\n",
      "\n"
     ]
    }
   ],
   "source": [
    "with warnings.catch_warnings(action=\"ignore\"): #Suppress warnings about Country samples with missing categories\n",
    "    makeappendtable('income')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "a0efb154-7d50-4b57-9e2a-39d847637b34",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "FULL SAMPLE RESULTS\n",
      "         employment  group_count     pc_all  pc_ifjobavail   pc_limit  \\\n",
      "0          Employed      49666.0  10.017692      32.608732  48.138332   \n",
      "1  Not in workforce      27350.0   9.882343      32.296291  46.269679   \n",
      "2        Unemployed       6551.0  10.742649      31.122208  45.485156   \n",
      "\n",
      "     pc_none  \n",
      "0   9.235244  \n",
      "1  11.551687  \n",
      "2  12.649988  \n",
      "\n",
      "Cross-tabulation of employment and immig_econ_level\n",
      " Number of strata: 1\n",
      " Number of PSUs: 83567\n",
      " Number of observations: 83567\n",
      " Degrees of freedom: 83566.00\n",
      "\n",
      "       employment immig_econ_level  point_est  stderror  lower_ci  upper_ci\n",
      "        Employed              all   0.059400  0.000916  0.057630  0.061220\n",
      "        Employed       ifjobavail   0.193352  0.001550  0.190332  0.196409\n",
      "        Employed            limit   0.285434  0.001725  0.282065  0.288828\n",
      "        Employed             none   0.054760  0.000864  0.053091  0.056478\n",
      "Not in workforce              all   0.031949  0.000667  0.030668  0.033282\n",
      "Not in workforce       ifjobavail   0.104412  0.001161  0.102157  0.106710\n",
      "Not in workforce            limit   0.149587  0.001360  0.146942  0.152272\n",
      "Not in workforce             none   0.037346  0.000717  0.035966  0.038777\n",
      "      Unemployed              all   0.008998  0.000355  0.008329  0.009720\n",
      "      Unemployed       ifjobavail   0.026068  0.000637  0.024848  0.027346\n",
      "      Unemployed            limit   0.038098  0.000733  0.036687  0.039562\n",
      "      Unemployed             none   0.010596  0.000385  0.009866  0.011378\n",
      "\n",
      "Pearson (with Rao-Scott adjustment):\n",
      "\tUnadjusted - chi2(6): 160.1839 with p-value of 0.0000\n",
      "\tAdjusted - F(0.00, 0.00): 0.0000  with p-value of nan\n",
      "\n",
      "  Likelihood ratio (with Rao-Scott adjustment):\n",
      "\t Unadjusted - chi2(6): 157.6795 with p-value of 0.0000\n",
      "\t Adjusted - F(0.00, 0.00): 0.0000  with p-value of nan\n",
      "\n"
     ]
    }
   ],
   "source": [
    "with warnings.catch_warnings(action=\"ignore\"): #Suppress warnings about Country samples with missing categories\n",
    "    makeappendtable('employment')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "7ade419b-4c38-4f19-87be-93f88e3f90e0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "FULL SAMPLE RESULTS\n",
      "         ideology  group_count     pc_all  pc_ifjobavail   pc_limit    pc_none\n",
      "0   Left ideology      33690.0  10.647219      35.310500  46.674897   7.367385\n",
      "1  Right ideology      29126.0  10.431264      32.553711  46.924388  10.090637\n",
      "\n",
      "Cross-tabulation of ideology and immig_econ_level\n",
      " Number of strata: 1\n",
      " Number of PSUs: 62816\n",
      " Number of observations: 62816\n",
      " Degrees of freedom: 62815.00\n",
      "\n",
      "       ideology immig_econ_level  point_est  stderror  lower_ci  upper_ci\n",
      " Left ideology              all   0.056604  0.001050  0.054580  0.058698\n",
      " Left ideology       ifjobavail   0.187721  0.001796  0.184227  0.191266\n",
      " Left ideology            limit   0.248138  0.001938  0.244359  0.251956\n",
      " Left ideology             none   0.039167  0.000857  0.037522  0.040882\n",
      "Right ideology              all   0.048857  0.000958  0.047013  0.050770\n",
      "Right ideology       ifjobavail   0.152472  0.001630  0.149305  0.155693\n",
      "Right ideology            limit   0.219780  0.001837  0.216201  0.223401\n",
      "Right ideology             none   0.047261  0.000948  0.045438  0.049154\n",
      "\n",
      "Pearson (with Rao-Scott adjustment):\n",
      "\tUnadjusted - chi2(3): 170.0525 with p-value of 0.0000\n",
      "\tAdjusted - F(0.00, 0.00): 0.0000  with p-value of nan\n",
      "\n",
      "  Likelihood ratio (with Rao-Scott adjustment):\n",
      "\t Unadjusted - chi2(3): 169.7846 with p-value of 0.0000\n",
      "\t Adjusted - F(0.00, 0.00): 0.0000  with p-value of nan\n",
      "\n"
     ]
    }
   ],
   "source": [
    "with warnings.catch_warnings(action=\"ignore\"): #Suppress warnings about Country samples with missing categories\n",
    "    makeappendtable('ideology')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "80a81f3e-e5b0-463e-b2d3-ea0545d8c0f5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "FULL SAMPLE RESULTS\n",
      "                  ntrlzd  group_count     pc_all  pc_ifjobavail   pc_limit  \\\n",
      "0  Locally-born national      77814.0  10.216906      31.191111  47.830097   \n",
      "1            Naturalized       2605.0   8.849858      41.011235  44.511136   \n",
      "2           Non-national       1847.0   8.276119      60.792813  28.734515   \n",
      "\n",
      "     pc_none  \n",
      "0  10.761886  \n",
      "1   5.627771  \n",
      "2   2.196554  \n",
      "\n",
      "Cross-tabulation of ntrlzd and immig_econ_level\n",
      " Number of strata: 1\n",
      " Number of PSUs: 82266\n",
      " Number of observations: 82266\n",
      " Degrees of freedom: 82265.00\n",
      "\n",
      "                ntrlzd immig_econ_level  point_est  stderror  lower_ci  upper_ci\n",
      "Locally-born national              all   0.097015  0.001141  0.094802  0.099274\n",
      "Locally-born national       ifjobavail   0.296177  0.001777  0.292707  0.299671\n",
      "Locally-born national            limit   0.454174  0.001917  0.450419  0.457934\n",
      "Locally-born national             none   0.102190  0.001154  0.099950  0.104474\n",
      "          Naturalized              all   0.002402  0.000177  0.002078  0.002776\n",
      "          Naturalized       ifjobavail   0.011130  0.000389  0.010394  0.011919\n",
      "          Naturalized            limit   0.012080  0.000418  0.011288  0.012927\n",
      "          Naturalized             none   0.001527  0.000150  0.001260  0.001852\n",
      "         Non-national              all   0.001929  0.000167  0.001627  0.002286\n",
      "         Non-national       ifjobavail   0.014167  0.000497  0.013226  0.015175\n",
      "         Non-national            limit   0.006696  0.000314  0.006107  0.007342\n",
      "         Non-national             none   0.000512  0.000083  0.000373  0.000703\n",
      "\n",
      "Pearson (with Rao-Scott adjustment):\n",
      "\tUnadjusted - chi2(6): 907.1923 with p-value of 0.0000\n",
      "\tAdjusted - F(0.00, 0.00): 0.0000  with p-value of nan\n",
      "\n",
      "  Likelihood ratio (with Rao-Scott adjustment):\n",
      "\t Unadjusted - chi2(6): 893.8236 with p-value of 0.0000\n",
      "\t Adjusted - F(0.00, 0.00): 0.0000  with p-value of nan\n",
      "\n"
     ]
    }
   ],
   "source": [
    "with warnings.catch_warnings(action=\"ignore\"): #Suppress warnings about Country samples with missing categories\n",
    "    makeappendtable('ntrlzd')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "0324aa9e-e9fb-4aa6-9848-ee5afb8024be",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "FULL SAMPLE RESULTS\n",
      "          ethnicplu  group_count     pc_all  pc_ifjobavail   pc_limit  \\\n",
      "0   Ethnic minority      28044.0   9.073117      32.962512  46.332275   \n",
      "1  Ethnic plurality      56317.0  10.577480      32.090555  47.726246   \n",
      "\n",
      "     pc_none  \n",
      "0  11.632096  \n",
      "1   9.605719  \n",
      "\n",
      "Cross-tabulation of ethnicplu and immig_econ_level\n",
      " Number of strata: 1\n",
      " Number of PSUs: 84361\n",
      " Number of observations: 84361\n",
      " Degrees of freedom: 84360.00\n",
      "\n",
      "        ethnicplu immig_econ_level  point_est  stderror  lower_ci  upper_ci\n",
      " Ethnic minority              all   0.029003  0.000620  0.027812  0.030243\n",
      " Ethnic minority       ifjobavail   0.105367  0.001168  0.103100  0.107677\n",
      " Ethnic minority            limit   0.148104  0.001320  0.145535  0.150711\n",
      " Ethnic minority             none   0.037183  0.000690  0.035854  0.038559\n",
      "Ethnic plurality              all   0.071963  0.000999  0.070029  0.073947\n",
      "Ethnic plurality       ifjobavail   0.218326  0.001611  0.215185  0.221500\n",
      "Ethnic plurality            limit   0.324702  0.001792  0.321199  0.328225\n",
      "Ethnic plurality             none   0.065352  0.000948  0.063519  0.067234\n",
      "\n",
      "Pearson (with Rao-Scott adjustment):\n",
      "\tUnadjusted - chi2(3): 126.4446 with p-value of 0.0000\n",
      "\tAdjusted - F(0.00, 0.00): 0.0000  with p-value of nan\n",
      "\n",
      "  Likelihood ratio (with Rao-Scott adjustment):\n",
      "\t Unadjusted - chi2(3): 125.6604 with p-value of 0.0000\n",
      "\t Adjusted - F(0.00, 0.00): 0.0000  with p-value of nan\n",
      "\n"
     ]
    }
   ],
   "source": [
    "with warnings.catch_warnings(action=\"ignore\"): #Suppress warnings about Country samples with missing categories\n",
    "    makeappendtable('ethnicplu')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "375897c7-147d-4486-bb6b-19951f652c9e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "FULL SAMPLE RESULTS\n",
      "                          bplXeth  group_count     pc_all  pc_ifjobavail  \\\n",
      "0    Born abroad, Ethnic minority       2172.0   9.192162      47.998198   \n",
      "1   Born abroad, Ethnic plurality       2280.0   8.112779      51.822162   \n",
      "2   Born locally, Ethnic minority      25264.0   8.989028      31.479381   \n",
      "3  Born locally, Ethnic plurality      52550.0  10.775903      31.059874   \n",
      "\n",
      "    pc_limit    pc_none  \n",
      "0  38.549527   4.260112  \n",
      "1  36.191467   3.873591  \n",
      "2  47.113039  12.418552  \n",
      "3  48.156541  10.007681  \n",
      "\n",
      "Cross-tabulation of bplXeth and immig_econ_level\n",
      " Number of strata: 1\n",
      " Number of PSUs: 82266\n",
      " Number of observations: 82266\n",
      " Degrees of freedom: 82265.00\n",
      "\n",
      "                        bplXeth immig_econ_level  point_est  stderror  lower_ci  upper_ci\n",
      "  Born abroad, Ethnic minority              all   0.002028  0.000156  0.001744  0.002358\n",
      "  Born abroad, Ethnic minority       ifjobavail   0.010588  0.000412  0.009811  0.011426\n",
      "  Born abroad, Ethnic minority            limit   0.008504  0.000340  0.007862  0.009197\n",
      "  Born abroad, Ethnic minority             none   0.000940  0.000117  0.000736  0.001199\n",
      " Born abroad, Ethnic plurality              all   0.002303  0.000187  0.001963  0.002701\n",
      " Born abroad, Ethnic plurality       ifjobavail   0.014709  0.000478  0.013801  0.015677\n",
      " Born abroad, Ethnic plurality            limit   0.010273  0.000397  0.009523  0.011081\n",
      " Born abroad, Ethnic plurality             none   0.001099  0.000125  0.000879  0.001375\n",
      " Born locally, Ethnic minority              all   0.026702  0.000600  0.025551  0.027904\n",
      " Born locally, Ethnic minority       ifjobavail   0.093511  0.001109  0.091361  0.095706\n",
      " Born locally, Ethnic minority            limit   0.139951  0.001300  0.137422  0.142520\n",
      " Born locally, Ethnic minority             none   0.036890  0.000693  0.035556  0.038272\n",
      "Born locally, Ethnic plurality              all   0.070313  0.000997  0.068383  0.072293\n",
      "Born locally, Ethnic plurality       ifjobavail   0.202666  0.001584  0.199579  0.205789\n",
      "Born locally, Ethnic plurality            limit   0.314222  0.001792  0.310720  0.317746\n",
      "Born locally, Ethnic plurality             none   0.065300  0.000958  0.063448  0.067203\n",
      "\n",
      "Pearson (with Rao-Scott adjustment):\n",
      "\tUnadjusted - chi2(9): 873.8705 with p-value of 0.0000\n",
      "\tAdjusted - F(0.00, 0.00): 0.0000  with p-value of nan\n",
      "\n",
      "  Likelihood ratio (with Rao-Scott adjustment):\n",
      "\t Unadjusted - chi2(9): 864.3788 with p-value of 0.0000\n",
      "\t Adjusted - F(0.00, 0.00): 0.0000  with p-value of nan\n",
      "\n"
     ]
    }
   ],
   "source": [
    "with warnings.catch_warnings(action=\"ignore\"): #Suppress warnings about Country samples with missing categories\n",
    "    makeappendtable('bplXeth')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "8af13e0a-be21-41a1-8517-4467d9628dc4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "FULL SAMPLE RESULTS\n",
      "       jobs  group_count     pc_all  pc_ifjobavail   pc_limit    pc_none\n",
      "0     Agree      58260.0   8.183194      28.400496  51.602609  11.813701\n",
      "1  Disagree      14826.0  16.460171      43.477675  33.715168   6.346985\n",
      "2   Neutral      10689.0  12.787264      41.833861  39.536242   5.842632\n",
      "\n",
      "Cross-tabulation of jobs and immig_econ_level\n",
      " Number of strata: 1\n",
      " Number of PSUs: 83775\n",
      " Number of observations: 83775\n",
      " Degrees of freedom: 83774.00\n",
      "\n",
      "     jobs immig_econ_level  point_est  stderror  lower_ci  upper_ci\n",
      "   Agree              all   0.059194  0.000907  0.057441  0.060997\n",
      "   Agree       ifjobavail   0.205438  0.001587  0.202344  0.208566\n",
      "   Agree            limit   0.373272  0.001853  0.369648  0.376910\n",
      "   Agree             none   0.085455  0.001062  0.083398  0.087559\n",
      "Disagree              all   0.026131  0.000609  0.024964  0.027351\n",
      "Disagree       ifjobavail   0.069022  0.000969  0.067146  0.070945\n",
      "Disagree            limit   0.053523  0.000851  0.051880  0.055216\n",
      "Disagree             none   0.010076  0.000362  0.009391  0.010810\n",
      " Neutral              all   0.015075  0.000467  0.014186  0.016019\n",
      " Neutral       ifjobavail   0.049318  0.000816  0.047743  0.050941\n",
      " Neutral            limit   0.046609  0.000779  0.045105  0.048161\n",
      " Neutral             none   0.006888  0.000318  0.006292  0.007539\n",
      "\n",
      "Pearson (with Rao-Scott adjustment):\n",
      "\tUnadjusted - chi2(6): 3314.1908 with p-value of 0.0000\n",
      "\tAdjusted - F(0.00, 0.00): 0.0000  with p-value of nan\n",
      "\n",
      "  Likelihood ratio (with Rao-Scott adjustment):\n",
      "\t Unadjusted - chi2(6): 3291.7349 with p-value of 0.0000\n",
      "\t Adjusted - F(0.00, 0.00): 0.0000  with p-value of nan\n",
      "\n"
     ]
    }
   ],
   "source": [
    "with warnings.catch_warnings(action=\"ignore\"): #Suppress warnings about Country samples with missing categories\n",
    "    makeappendtable('jobs')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "79e444eb-0e28-46d9-81c5-bc1d76c6c845",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "FULL SAMPLE RESULTS\n",
      "                                  placeid  group_count     pc_all  \\\n",
      "0                   Closest international       2214.0  17.158976   \n",
      "1      Closest international and national       3086.0  11.013543   \n",
      "2                        Closest national       6436.0   6.442341   \n",
      "3                    Closest sub-national      17578.0  11.149111   \n",
      "4  Closest sub-national and international       2678.0  16.094968   \n",
      "5       Closest sub-national and national      24144.0   8.058511   \n",
      "6                       Equally close all      25583.0  10.567524   \n",
      "7                      Not close anywhere       2282.0  13.132139   \n",
      "\n",
      "   pc_ifjobavail   pc_limit    pc_none  \n",
      "0      38.148125  36.881927   7.810971  \n",
      "1      35.446314  46.802860   6.737283  \n",
      "2      32.657311  51.920145   8.980202  \n",
      "3      32.386806  46.459512  10.004570  \n",
      "4      36.883187  37.430354   9.591491  \n",
      "5      30.596098  50.674659  10.670731  \n",
      "6      32.950694  45.830176  10.651607  \n",
      "7      30.741421  43.204289  12.922151  \n",
      "\n",
      "Cross-tabulation of placeid and immig_econ_level\n",
      " Number of strata: 1\n",
      " Number of PSUs: 84001\n",
      " Number of observations: 84001\n",
      " Degrees of freedom: 84000.00\n",
      "\n",
      "                                placeid immig_econ_level  point_est  stderror  lower_ci  upper_ci\n",
      "    Closest international and national              all   0.003561  0.000252  0.003100  0.004091\n",
      "    Closest international and national       ifjobavail   0.011462  0.000397  0.010710  0.012267\n",
      "    Closest international and national            limit   0.015135  0.000438  0.014300  0.016018\n",
      "    Closest international and national             none   0.002179  0.000169  0.001872  0.002536\n",
      "                 Closest international              all   0.004151  0.000246  0.003695  0.004663\n",
      "                 Closest international       ifjobavail   0.009229  0.000363  0.008544  0.009968\n",
      "                 Closest international            limit   0.008923  0.000348  0.008266  0.009631\n",
      "                 Closest international             none   0.001890  0.000154  0.001610  0.002218\n",
      "                      Closest national              all   0.004261  0.000254  0.003791  0.004788\n",
      "                      Closest national       ifjobavail   0.021597  0.000540  0.020564  0.022682\n",
      "                      Closest national            limit   0.034337  0.000654  0.033078  0.035641\n",
      "                      Closest national             none   0.005939  0.000272  0.005429  0.006497\n",
      "Closest sub-national and international              all   0.004681  0.000253  0.004211  0.005204\n",
      "Closest sub-national and international       ifjobavail   0.010728  0.000382  0.010004  0.011504\n",
      "Closest sub-national and international            limit   0.010887  0.000390  0.010148  0.011679\n",
      "Closest sub-national and international             none   0.002790  0.000188  0.002444  0.003185\n",
      "     Closest sub-national and national              all   0.023783  0.000584  0.022666  0.024954\n",
      "     Closest sub-national and national       ifjobavail   0.090299  0.001105  0.088156  0.092488\n",
      "     Closest sub-national and national            limit   0.149557  0.001367  0.146897  0.152256\n",
      "     Closest sub-national and national             none   0.031493  0.000671  0.030205  0.032834\n",
      "                  Closest sub-national              all   0.023022  0.000567  0.021936  0.024160\n",
      "                  Closest sub-national       ifjobavail   0.066875  0.000987  0.064966  0.068837\n",
      "                  Closest sub-national            limit   0.095934  0.001107  0.093787  0.098126\n",
      "                  Closest sub-national             none   0.020658  0.000528  0.019649  0.021719\n",
      "                     Equally close all              all   0.033954  0.000694  0.032620  0.035342\n",
      "                     Equally close all       ifjobavail   0.105873  0.001220  0.103506  0.108288\n",
      "                     Equally close all            limit   0.147256  0.001372  0.144587  0.149966\n",
      "                     Equally close all             none   0.034224  0.000696  0.032887  0.035615\n",
      "                    Not close anywhere              all   0.003325  0.000217  0.002926  0.003779\n",
      "                    Not close anywhere       ifjobavail   0.007784  0.000328  0.007167  0.008455\n",
      "                    Not close anywhere            limit   0.010940  0.000385  0.010212  0.011720\n",
      "                    Not close anywhere             none   0.003272  0.000216  0.002875  0.003724\n",
      "\n",
      "Pearson (with Rao-Scott adjustment):\n",
      "\tUnadjusted - chi2(21): 766.4702 with p-value of 0.0000\n",
      "\tAdjusted - F(0.00, 0.00): 0.0000  with p-value of nan\n",
      "\n",
      "  Likelihood ratio (with Rao-Scott adjustment):\n",
      "\t Unadjusted - chi2(21): 759.7950 with p-value of 0.0000\n",
      "\t Adjusted - F(0.00, 0.00): 0.0000  with p-value of nan\n",
      "\n"
     ]
    }
   ],
   "source": [
    "with warnings.catch_warnings(action=\"ignore\"): #Suppress warnings about Country samples with missing categories\n",
    "    makeappendtable('placeid')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "b8d95fa2-8eec-4f9e-9549-9e723a266942",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "FULL SAMPLE RESULTS\n",
      "    impact  group_count     pc_all  pc_ifjobavail   pc_limit    pc_none\n",
      "0      Bad      25735.0   6.463635      21.641512  53.755564  18.139289\n",
      "1     Good      24129.0  14.987942      41.456004  37.298352   6.257703\n",
      "2  Neutral      32957.0   9.513950      34.839010  49.201222   6.445818\n",
      "\n",
      "Cross-tabulation of impact and immig_econ_level\n",
      " Number of strata: 1\n",
      " Number of PSUs: 82821\n",
      " Number of observations: 82821\n",
      " Degrees of freedom: 82820.00\n",
      "\n",
      "  impact immig_econ_level  point_est  stderror  lower_ci  upper_ci\n",
      "    Bad              all   0.020867  0.000543  0.019829  0.021959\n",
      "    Bad       ifjobavail   0.069868  0.001049  0.067840  0.071952\n",
      "    Bad            limit   0.173546  0.001443  0.170737  0.176392\n",
      "    Bad             none   0.058561  0.000888  0.056845  0.060326\n",
      "   Good              all   0.041212  0.000766  0.039736  0.042741\n",
      "   Good       ifjobavail   0.113991  0.001229  0.111605  0.116422\n",
      "   Good            limit   0.102559  0.001175  0.100279  0.104885\n",
      "   Good             none   0.017207  0.000504  0.016246  0.018223\n",
      "Neutral              all   0.038264  0.000748  0.036825  0.039756\n",
      "Neutral       ifjobavail   0.140118  0.001341  0.137510  0.142767\n",
      "Neutral            limit   0.197881  0.001528  0.194903  0.200894\n",
      "Neutral             none   0.025924  0.000607  0.024761  0.027140\n",
      "\n",
      "Pearson (with Rao-Scott adjustment):\n",
      "\tUnadjusted - chi2(6): 5706.4053 with p-value of 0.0000\n",
      "\tAdjusted - F(0.00, 0.00): 0.0000  with p-value of nan\n",
      "\n",
      "  Likelihood ratio (with Rao-Scott adjustment):\n",
      "\t Unadjusted - chi2(6): 5595.0834 with p-value of 0.0000\n",
      "\t Adjusted - F(0.00, 0.00): 0.0000  with p-value of nan\n",
      "\n"
     ]
    }
   ],
   "source": [
    "with warnings.catch_warnings(action=\"ignore\"): #Suppress warnings about Country samples with missing categories\n",
    "    makeappendtable('impact')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "c967448b-4f1b-4872-b1e7-c0059d83fd33",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "FULL SAMPLE RESULTS\n",
      "         trust_foreigners  group_count     pc_all  pc_ifjobavail   pc_limit  \\\n",
      "0  Don't trust foreigners      47943.0   8.854894      28.309760  49.749657   \n",
      "1        Trust foreigners      33137.0  12.185678      38.663674  43.258038   \n",
      "\n",
      "     pc_none  \n",
      "0  13.085688  \n",
      "1   5.892610  \n",
      "\n",
      "Cross-tabulation of trust_foreigners and immig_econ_level\n",
      " Number of strata: 1\n",
      " Number of PSUs: 81080\n",
      " Number of observations: 81080\n",
      " Degrees of freedom: 81079.00\n",
      "\n",
      "       trust_foreigners immig_econ_level  point_est  stderror  lower_ci  upper_ci\n",
      "Don't trust foreigners              all   0.054803  0.000888  0.053089  0.056570\n",
      "Don't trust foreigners       ifjobavail   0.175211  0.001501  0.172288  0.178173\n",
      "Don't trust foreigners            limit   0.307904  0.001796  0.304394  0.311436\n",
      "Don't trust foreigners             none   0.080988  0.001052  0.078951  0.083073\n",
      "      Trust foreigners              all   0.046439  0.000818  0.044861  0.048070\n",
      "      Trust foreigners       ifjobavail   0.147345  0.001408  0.144606  0.150126\n",
      "      Trust foreigners            limit   0.164854  0.001433  0.162065  0.167681\n",
      "      Trust foreigners             none   0.022456  0.000567  0.021371  0.023596\n",
      "\n",
      "Pearson (with Rao-Scott adjustment):\n",
      "\tUnadjusted - chi2(3): 1972.1266 with p-value of 0.0000\n",
      "\tAdjusted - F(0.00, 0.00): 0.0000  with p-value of nan\n",
      "\n",
      "  Likelihood ratio (with Rao-Scott adjustment):\n",
      "\t Unadjusted - chi2(3): 2037.8793 with p-value of 0.0000\n",
      "\t Adjusted - F(0.00, 0.00): 0.0000  with p-value of nan\n",
      "\n"
     ]
    }
   ],
   "source": [
    "with warnings.catch_warnings(action=\"ignore\"): #Suppress warnings about Country samples with missing categories\n",
    "    makeappendtable('trust_foreigners')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "882d8fdd-d39b-4e6d-89cc-1d98560d2daa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "FULL SAMPLE RESULTS\n",
      "       foreignneighbors_OK  group_count     pc_all  pc_ifjobavail   pc_limit  \\\n",
      "0      Foreign neighbor OK      65404.0  10.572013      34.979478  45.897902   \n",
      "1  Foreign neighbor not OK      18021.0   8.209089      23.613959  52.192028   \n",
      "\n",
      "     pc_none  \n",
      "0   8.550608  \n",
      "1  15.984924  \n",
      "\n",
      "Cross-tabulation of foreignneighbors_OK and immig_econ_level\n",
      " Number of strata: 1\n",
      " Number of PSUs: 83425\n",
      " Number of observations: 83425\n",
      " Degrees of freedom: 83424.00\n",
      "\n",
      "     foreignneighbors_OK immig_econ_level  point_est  stderror  lower_ci  upper_ci\n",
      "    Foreign neighbor OK              all   0.081863  0.001059  0.079811  0.083964\n",
      "    Foreign neighbor OK       ifjobavail   0.270861  0.001715  0.267513  0.274234\n",
      "    Foreign neighbor OK            limit   0.355406  0.001834  0.351819  0.359010\n",
      "    Foreign neighbor OK             none   0.066211  0.000946  0.064382  0.068088\n",
      "Foreign neighbor not OK              all   0.018525  0.000505  0.017560  0.019541\n",
      "Foreign neighbor not OK       ifjobavail   0.053287  0.000922  0.051509  0.055123\n",
      "Foreign neighbor not OK            limit   0.117776  0.001221  0.115404  0.120190\n",
      "Foreign neighbor not OK             none   0.036071  0.000704  0.034717  0.037476\n",
      "\n",
      "Pearson (with Rao-Scott adjustment):\n",
      "\tUnadjusted - chi2(3): 1571.7464 with p-value of 0.0000\n",
      "\tAdjusted - F(0.00, 0.00): 0.0000  with p-value of nan\n",
      "\n",
      "  Likelihood ratio (with Rao-Scott adjustment):\n",
      "\t Unadjusted - chi2(3): 1537.2642 with p-value of 0.0000\n",
      "\t Adjusted - F(0.00, 0.00): 0.0000  with p-value of nan\n",
      "\n"
     ]
    }
   ],
   "source": [
    "with warnings.catch_warnings(action=\"ignore\"): #Suppress warnings about Country samples with missing categories\n",
    "    makeappendtable('foreignneighbors_OK')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "48e722a3-4293-439a-83b8-b50dfb12cc05",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "FULL SAMPLE RESULTS\n",
      "           ntnlpride  group_count     pc_all  pc_ifjobavail   pc_limit  \\\n",
      "0          Not proud       8754.0  11.537546      33.568474  44.725170   \n",
      "1  Other nationality       1452.0   9.328090      60.167404  28.414210   \n",
      "2              Proud      73256.0   9.907505      31.573927  48.037569   \n",
      "\n",
      "     pc_none  \n",
      "0  10.168810  \n",
      "1   2.090297  \n",
      "2  10.480999  \n",
      "\n",
      "Cross-tabulation of ntnlpride and immig_econ_level\n",
      " Number of strata: 1\n",
      " Number of PSUs: 83462\n",
      " Number of observations: 83462\n",
      " Degrees of freedom: 83461.00\n",
      "\n",
      "         ntnlpride immig_econ_level  point_est  stderror  lower_ci  upper_ci\n",
      "        Not proud              all   0.011639  0.000390  0.010899  0.012428\n",
      "        Not proud       ifjobavail   0.033863  0.000697  0.032524  0.035256\n",
      "        Not proud            limit   0.045118  0.000784  0.043606  0.046680\n",
      "        Not proud             none   0.010258  0.000390  0.009521  0.011051\n",
      "Other nationality              all   0.001700  0.000155  0.001422  0.002032\n",
      "Other nationality       ifjobavail   0.010965  0.000429  0.010156  0.011838\n",
      "Other nationality            limit   0.005178  0.000303  0.004618  0.005807\n",
      "Other nationality             none   0.000381  0.000079  0.000253  0.000573\n",
      "            Proud              all   0.087275  0.001092  0.085158  0.089440\n",
      "            Proud       ifjobavail   0.278134  0.001737  0.274742  0.281551\n",
      "            Proud            limit   0.423162  0.001893  0.419455  0.426877\n",
      "            Proud             none   0.092327  0.001095  0.090203  0.094496\n",
      "\n",
      "Pearson (with Rao-Scott adjustment):\n",
      "\tUnadjusted - chi2(6): 639.5539 with p-value of 0.0000\n",
      "\tAdjusted - F(0.00, 0.00): 0.0000  with p-value of nan\n",
      "\n",
      "  Likelihood ratio (with Rao-Scott adjustment):\n",
      "\t Unadjusted - chi2(6): 632.2568 with p-value of 0.0000\n",
      "\t Adjusted - F(0.00, 0.00): 0.0000  with p-value of nan\n",
      "\n"
     ]
    }
   ],
   "source": [
    "with warnings.catch_warnings(action=\"ignore\"): #Suppress warnings about Country samples with missing categories\n",
    "    makeappendtable('ntnlpride')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
